package edu.unlv.cs.ladders.entities;

import edu.unlv.cs.ladders.entities.BaseEntity;
import javax.persistence.Entity;
import javax.persistence.OneToOne;
import javax.persistence.ManyToOne;

import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.roo.addon.entity.RooEntity;

@Entity
@RooJavaBean
@RooToString
@RooEntity
public class Score extends BaseEntity implements Comparable<Score> {
	private static final long serialVersionUID = 1L;

    @ManyToOne
    private Team team;
    @OneToOne
    private Event event;
    private Integer score = 0;
    private Boolean accepted = true;
    
    public Team getOpponent(){
    	Score challenger = this.getEvent().getChallengerScore();
    	if(this.equals(challenger)){
    		return this.getEvent().getDefender();
    	} else {
    		return this.getEvent().getChallenger();
    	}
    }
    
    @Override
    public String toString(){
    	return this.getTeam().getName() + " scored "+ score +" against " + getOpponent().getName();
    }

	public int compareTo(Score o) {
		if(getScore()<o.getScore()){
			return -1;
		} else if(getScore()>o.getScore()){
			return 1;
		}
		return 0;
	}
    
}
